

$(function () {
    var name = $("#name");
    var pic = $("#i");
    var fileName = $("#fileName");
    var price = $("#price");
    var stock = $("#stock");
    var desc = $("#description");
    var update = $("#update");
    var productId = $("#productId");
    var img = $("#img");

    var l1 = $("#L1");
    var l2 = $("#L2");
    var l3 = $("#L3");

    var j1 = $("#j1");
    var j2 = $("#j2");
    var j3 = $("#j3");
    fillL2();
    fillL3();
    l1.change(function () {
        fillL2();
        fillL3();
    });
    l2.change(function () {
        fillL3();
    });

    function fillL2(){
        var pcode = l1.val();
        $.ajax({
            async : false,
            url : "easybuy/product/getType",
            data : {"id":pcode},
            type : "post",
            dataType: "json",
            success : function (data) {
                var str = "";
                l2.html("");
                for(var i=0;i<data.length;i++){
                    str += "<option value='"+data[i].id+"'>"+data[i].name+"</option>"
                }
                l2.append(str);
            }
        });

    }
    function fillL3(){
        var pcode = l2.val();
        $.ajax({
            async : false,
            url : "easybuy/product/getType",
            data : {"id":pcode},
            type : "post",
            dataType: "json",
            success : function (data) {
                var str = "";
                l3.html("");
                for(var i=0 ; i<data.length; i++){
                    str += "<option value='"+data[i].id+"'>"+data[i].name+"</option>"
                }
                l3.append(str);
            }
        });

    }

    $.ajax({
        url: "easybuy/product/getProduct",
        dataType: "json",
        type: "post",
        data: {"id": jq.query.get("id")},
        success: function (data) {
            productId.attr("value", jq.query.get("id"));
            name.attr("value", data.name);
            price.attr("value", data.price);
            stock.attr("value", data.stock);
            desc.val(data.description);
            j1.attr("value",data.categoryLevel1Id);
            j2.attr("value",data.categoryLevel2Id);
            j3.attr("value",data.categoryLevel3Id);

            pic.append("<img id='img' src='../images/" + data.fileName + "' style='width: 100px;height: 100px;border: 1px solid gray'>");
            var ll1 = ["548","628","660","670","676","681"];
            var ll2 = ["654","656","657","661","670","674","677","678","679","682","683","690","696"];
            var ll3 = ["655","658","659","662","663","669","672","673","675","680","684","685","686","687","688","689","691","692","693"];

            var jb1 = j1.val();
            var jb2 = j2.val();
            var jb3 = j3.val();
            if(jb1 !=null){
                for(var aa= 0 ;aa<ll1.length; aa++){
                    if(ll1[aa] == jb1){
                        console.log(jb1);
                        //$("#L1 option[value='"++"']").removeAttr("selected");
                        //$("#L1").find("option[value = '"+jb1+"']").attr("selected","selected");
                        $("#L1 option[value='"+jb1+"']").attr("selected","selected");
                        fillL2();
                        break;
                    }
                }
            }

            if(jb2>0){
                for(var bb = 0;bb<ll2.length; bb++){
                    if(ll2[bb] == j2.val()){
                        //console.log(jb2);
                        $("#L2 option[value='"+jb2+"']").attr("selected","selected");
                        fillL3();
                        break;
                    }
                }
            }
            if(jb3>0){
                for(var cc = 0; cc<ll3.length; cc++){
                    if(ll3[cc] == j3.val()){
                        //console.log(j3.val());
                        $("#L3 option[value='"+jb3+"']").attr("selected","selected");
                        break;
                    }
                }
            }
        },
        error: function () {
            alert("出现错误");
        }

    });


    /*fileName.change(function () {
        img.hide();
        fileName.change(function () {
            var fileReader = new FileReader();
            fileReader.onload = function (e) {
                img.show();
                img.attr('src', e.target.result);
            };
            var imageFile = this.files[0];
            fileReader.readAsDataURL(imageFile);

        });
    });*/

});















